<?php
class energyController extends base
{

    public function __construct()
    {
        parent::__construct();
    }
    
    /*
     * 获取具体帖子可打赏数值和用户余额
     */
    public function douserCanPayPost(){
        $news_id = common::request('news_id','G');
        $uid = $this->uid;
        
        if(!$uid){
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!$news_id){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $redisKey = REDIS_PREFIX.'PostPayEnergyList';
        $payEnergyList = $this->redis->exists($redisKey) ? json_decode($this->redis->get($redisKey),true) : [0=>'[1,3,5,7,9]'];
        $can_pay_list = isset($payEnergyList[$news_id]) ? json_decode($payEnergyList[$news_id],true) : json_decode($payEnergyList[0],true);
        
        $userInfo = $this->load('client')->getUserInfo($uid);
        
        $this->result['result'] = [
            'total_energy' => $userInfo['total_energy'],
            'can_pay_list' => $can_pay_list
        ];
    }
    
    /*
     * 获得帖子打赏列表
     */
    public function dopostEnergyRecord(){
        $news_id = common::request('news_id','G');
        $news_uid = common::request('news_uid','G');
        $record_id = common::request('record_id','G',0);
        $limit = common::request('limit','G',20);
        
        if(!$news_id){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        if(!$news_uid){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $sql = "select count(*) from r_energy_record WHERE news_id={$news_id} AND uid={$news_uid} AND `type`='paid'";
        $record_count = $this->db->getOne($sql);
        
        if($record_count){
            $sql = "SELECT * FROM r_energy_record WHERE news_id={$news_id} AND uid={$news_uid}";
            if($record_id){
                $sql .= " AND record_id<{$record_id}";
            }
            $sql .= " ORDER BY record_id DESC limit {$limit}";
            $rows = $this->db->getAll($sql);
            if($rows){
                $client_mod = $this->load('client');
                foreach($rows as &$row){
                    if(!$row['target_uid']){
                        continue;
                    }
                    $row['from_user'] = $client_mod->getUserInfo($row['target_uid']);
                }
            }
        }
        $this->result['result'] = [
            'record_count' => $record_count,
            'records' => isset($rows) ? $rows : []
        ];
    }
    
    /*
     * 打赏正能量
     */
    public function dopay(){
        $target_uid = common::request('to_uid','P');
        $news_id = common::request('news_id','P');
        $energy_add = abs((int)common::request('energy','P'));
        $uid = $this->uid;
        
        if(!$news_id || !$target_uid || !$energy_add){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        if(!$uid){
            $_ENV['error']->getErrors(201);
            exit;
        }
        
        if($uid == $target_uid){
            $_ENV['error']->getErrors(4003);
            exit;
        }
        
        $client_mod = $this->load('client');
        $userInfo = $client_mod->getUserInfo($uid);
        if($userInfo['total_energy'] < $energy_add){
            $_ENV['error']->getErrors(4002);
            exit;
        }
        
        if($client_mod->isPostPaidBy($news_id,$uid)){
            $_ENV['error']->getErrors(4001);
            exit;
        }
        
        $client_mod->userEnergyChange($uid,'pay',[
            'target_uid' => $target_uid,
            'news_id' => $news_id,
            'energy_add' => -$energy_add
        ]);
        
        $client_mod->userEnergyChange($target_uid,'paid',[
            'target_uid'=>$uid,
            'news_id'=>$news_id,
            'energy_add'=>$energy_add
        ]);
        
        $bbs_mod = $this->load('bbs');
        $bbs_mod->addUnReadMsg($target_uid, array('msgType'=>5, 'sponsor'=>array('uid'=>$uid,'comment_id'=>'0'), 'quote'=>array('news_id'=>$news_id,'comment_id'=>'0')));

        //发送IM消息
        $postInfo = $bbs_mod->getPostInfo($news_id);
        $category = "2";
        $send_id = "S10002";
        $descType = "N10";
        $contentArr = [
            "news_id" => $news_id,
            "content" => $postInfo['content'],
            "energy" => $energy_add
        ];
        $content = json_encode($contentArr);
        $this->load('message')->insertMessage($category,$send_id,$uid,$target_uid,$descType,$content);        
        
        //是否获得勋章
        if($this->afterVersion('2.9.1','2.9.1')){
            $medal_id = $this->load('medal')->addMedalByTag($uid,'payEnergy');
            if($medal_id){
                $this->result['result']['medal_id'] = [$medal_id];
            }
        }
        
        $this->result['result']['is_success'] = 1;
    }
}